package com.clkj.xiuzheng.controller;

import com.clkj.xiuzheng.dao.UserMapper;
import com.clkj.xiuzheng.pojo.*;
import com.clkj.xiuzheng.pojo.common.Result;
import com.clkj.xiuzheng.pojo.common.StatusCode;
import com.clkj.xiuzheng.service.UserService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author yawang
 * @create 2024/04/11 17:06:23
 **/
@RestController
@RequestMapping(value = "/user")
@Api(value = "/user", tags = {"用户管理"})
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;

    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public Result login(@ApiParam(name = "code", value = "code值") @RequestParam("code") String code){
        try {
            Map map = userService.login(code);
            return new Result(true, StatusCode.OK,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation(value = "添加意向单")
    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody User user){
        try {
            Map map = userService.addOrder(user);
            return new Result(true, StatusCode.OK,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation(value = "获取推广员的意向单 传0为查全部")
    @PostMapping("/getSaleUserOrder")
    public Result getSaleUserOrder(Integer promoterId){
        try {
            List<UserPut> userPuts = userService.getSaleUserOrder(promoterId);
            return new Result(true, StatusCode.OK,"查询成功",userPuts);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation(value = "添加推广员负责的区域")
    @PostMapping("/addpromoterArea")
    public Result addpromoterArea(String  areaIdList,@RequestParam Integer userId){
        try {
            userService.addpromoterArea(areaIdList,userId);
            return new Result(true, StatusCode.OK,"查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation(value = "获取我的所有负责的区域")
    @PostMapping("/listMyArea")
    public Result listMyArea(@RequestParam Integer userId){
        try {
            List<Area> areas = userService.listMyArea(userId);
            return new Result(true, StatusCode.OK,"查询成功",areas);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }


    @ApiOperation(value = "更改意向单是否已联系")
    @PostMapping("/updateUserPutIsContant")
    public Result updateUserPutIsContant(@RequestParam Integer recordId){
        try {
            userService.updateUserPutIsContant(recordId);
            return new Result(true, StatusCode.OK,"查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation(value = "给存在的意向单添加回执信息")
    @PostMapping("/updateUserPutReceiptContent")
    public Result updateUserPutReceiptContent(Integer recordId,String receiptContent){
        try {
            userService.updateUserPutReceiptContent(recordId,receiptContent);
            return new Result(true, StatusCode.OK,"查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }



    @ApiOperation(value = "获取我的投放信息")
    @PostMapping("/listMyUserPut")
    public Result listMyUserPut(Integer userId){
        try {
            Map map = userService.listMyUserPut(userId);
            return new Result(true, StatusCode.OK,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation(value = "根据投放记录获取投放的店铺")
    @PostMapping("/listMyPutShop")
    public Result listMyPutShop(Integer recordId){
        try {
            List<Shop> Shops = userService.listMyPutShop(recordId);
            return new Result(true, StatusCode.OK,"查询成功",Shops);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation(value = "审核")
    @PostMapping("/applyUserPut")
    public Result applyUserPut(Integer recordId){
        try {
            userService.applyUserPut(recordId);
            return new Result(true, StatusCode.OK,"查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation(value = "查询所有用户")
    @PostMapping("/getAllUser")
    public Result getAllUser(@RequestParam Integer pageSize,
                             @RequestParam Integer pageNum){
        try {
           PageInfo pageInfo = userService.getAllUser(pageSize,pageNum);
            return new Result(true, StatusCode.OK,"查询成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation("查询销售员主页基本信息")
    @PostMapping("/getUserInfo")
    public Result getUserInfo(@RequestParam Integer userId){
        try {
            UserInfoVo userInfo = userService.getUserInfo(userId);
            return new Result(true, StatusCode.OK,"查询成功",userInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation("查询所有销售员今日销售以及今日拜访排名")
    @PostMapping("/getUserSaleRanking")
    public Result getUserSaleRanking(@RequestParam Integer companyId){
        try {
           Map map = userService.getUserSaleRanking(companyId);
            return new Result(true, StatusCode.OK,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation("获取所有操作员信息")
    @PostMapping("/getSaleUser")
    public Result getSaleUser(Integer companyId,
                              @ApiParam(name="flag",value="0查询业务员和操作人，1查询广告牌推广员") @RequestParam Integer flag){
        try {
            List<User> users = userService.getSaleUser(companyId,flag);
            return new Result(true, StatusCode.OK,"查询成功",users);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation("添加操作员")
    @PostMapping("/addSaleUser")
    public Result addSaleUser(@RequestBody User user,String areaIdList){
        try {
            userService.addUser(user,areaIdList);
            return new Result(true, StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }
    }
    @ApiOperation("启用、禁用操作员")
    @PostMapping("/updateSaleUserUp")
    public Result updateSaleUserUp(@RequestParam Integer userId,Integer status){
        try {
            userService.updateUserUp(userId,status);
            return new Result(true, StatusCode.OK,"启用成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"启用失败");
        }
    }

    @ApiOperation("广告牌业务员登录")
    @PostMapping("/advLogin")
    public Result advLogin(String userTel,String userPass){
        try {
            Map map = userService.advLogin(userTel,userPass);
            return new Result(true, StatusCode.OK,"启用成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"启用失败");
        }
    }

    @ApiOperation(value = "广告牌获取业务员负责区域及该业务员所负责区域内没有投放的所有门店")
    @PostMapping("/listUserAdvShop")
    public Result listUserAdvShop(Integer userId){
        try {
            Map map = userService.listUserAdvShop(userId);
            return new Result(true, StatusCode.OK,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation(value = "意向单定金支付")
    @PostMapping("/payOrder")
    public Result payOrder(Integer recordId, String payMoney, String openId, HttpServletRequest request){
        try {
            Map map = userService.payOrder(recordId,payMoney,openId,request);
            return new Result(true, StatusCode.OK,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }


    @ApiOperation(value = "意向单定金支付")
    @PostMapping("/paySuccess")
    public Result paySuccess(HttpServletRequest request, HttpServletResponse response){
        try {
            userService.paySuccess(request,response);
            return new Result(true, StatusCode.OK,"查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation(value = "工人提交基本信息(打卡)")
    @PostMapping("/addUserVisit")
    public Result addUserVisit(ShopVisit shopVisit,HttpServletRequest request){
        try {
            userService.addUserVisit(shopVisit,request);
            return new Result(true, StatusCode.OK,"查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation(value = "工人数据统计")
    @PostMapping("/shopVisitCount")
    public Result shopVisitCount(Integer userId){
        try {
            Map map = userService.shopVisitCount(userId);
            return new Result(true, StatusCode.OK,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation(value = "获取全部打卡记录")
    @PostMapping("/listShopVisit")
    public Result listShopVisit(Integer page,Integer size,Integer userId,Integer status){
        try {
            PageInfo pageInfo  = userService.listShopVisit(page,size,userId,status);
            return new Result(true, StatusCode.OK,"查询成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation(value = "打卡审核")
    @PostMapping("/applyShopVisit")
    public Result applyShopVisit(String ids,Integer status,String applyNote){
        try {
            userService.applyShopVisit(ids,status,applyNote);
            return new Result(true, StatusCode.OK,"查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation("工人余额纪录")
    @PostMapping("/UserBalance")
    public Result UserBalance(Integer userId){
        try {
           Double userBalance = userService.UserBalance(userId);
            return new Result(true, StatusCode.OK,"查询成功",userBalance);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation("获取业务员二维码")
    @PostMapping("/listUserImg")
    public Result listUserImg(Integer userId){
        try {
            UserImg userImg = userService.listUserImg(userId);
            return new Result(true, StatusCode.OK,"查询成功",userImg);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation("修改密码")
    @PostMapping("/updatePass")
    public Result updatePass(Integer userId,String pass){
        try {
            userService.updatePass(userId,pass);
            return new Result(true, StatusCode.OK,"查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation("获取工人某月的佣金信息")
    @PostMapping("/listGMoney")
    public Result listGMoney(String time,Integer userId){
        try {
            List<User> users = userService.listGMoney(time,userId);
            return new Result(true, StatusCode.OK,"查询成功",users);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }


    @ApiOperation("测试")
    @PostMapping("/test")
    public Result test(Integer flag){
        try {
            userService.test(flag);
            return new Result(true, StatusCode.OK,"查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }

    @ApiOperation("用户注册")
    @PostMapping("/addMiniUser")
    public Result addMiniUser(String userName,String UserPassword,String userTel){
        try {
            String type = userService.addMiniUser(userName,UserPassword,userTel);
            if(type.equals("0")){
                return new Result(true, StatusCode.OK,"用户已经存在");
            }else {
                return new Result(true, StatusCode.OK,"注册成功");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"注册失败");
        }
    }

    @ApiOperation("用户登录")
    @PostMapping("/userMinilogin")
    public Result userMinilogin(String userTel,String UserPassword){
        try {
            UserMini userMini  = userService.userMinilogin(userTel,UserPassword);
            return new Result(true, StatusCode.OK,"登录成功",userMini);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"登录失败");
        }
    }




}
